<template>
    <div>
        <div class="page-title">模板消息</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="模板标题.">
                <el-input ref="first" v-model="form.first" placeholder="请输入模板标题"></el-input>
            </el-form-item>

            <el-form-item label="关键词1.">
                <el-input ref="keyword1" v-model="form.keyword1" placeholder="请输入关键词1"></el-input>
            </el-form-item>

            <el-form-item label="关键词2.">
                <el-input ref="keyword2" v-model="form.keyword2" placeholder="请输入关键词2"></el-input>
            </el-form-item>

            <el-form-item label="模板备注.">
                <el-input ref="remark" v-model="form.remark" placeholder="请输入模板备注"></el-input>
            </el-form-item>

            <el-form-item label="跳转地址.">
                <el-input ref="url" v-model="form.url" placeholder="请输入跳转地址"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit">保存</el-button>
                <el-button type="primary" icon="el-icon-s-promotion" @click="send">推送</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        get,
        put,
        post
    } from '@/network/request'

    export default {
        name: 'Template',
        data() {
            return {
                form: {
                    cid: sessionStorage.getItem('cid'),
                    first: '',
                    keyword1: '',
                    keyword2: '',
                    remark: '',
                    url: ''
                }
            }
        },
        mounted() {
            this.$nextTick(() => {
                // 获取信息
                get('/getTemplate', {
                    cid: sessionStorage.getItem('cid')
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.form.first = result.data.first
                        this.form.keyword1 = result.data.keyword1
                        this.form.keyword2 = result.data.keyword2
                        this.form.remark = result.data.remark
                        this.form.url = result.data.url
                    }
                }).catch()
            })
        },
        methods: {
            // 提交
            submit() {
                if (this.form.first == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入模板标题',
                        offset: 70
                    })
                    this.$refs['first'].focus()
                    return false
                }

                if (this.form.keyword1 == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入关键词1',
                        offset: 70
                    })
                    this.$refs['keyword1'].focus()
                    return false
                }

                if (this.form.keyword2 == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入关键词2',
                        offset: 70
                    })
                    this.$refs['keyword2'].focus()
                    return false
                }

                if (this.form.remark == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入模板备注',
                        offset: 70
                    })
                    this.$refs['remark'].focus()
                    return false
                }

                put('/modifyTemplate', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.$notify.success({
                            title: '系统提示',
                            message: '模板消息修改成功',
                            offset: 70
                        })
                        this.$router.push('/template')
                    }
                }).catch()
            },
            // 发送模板
            send() {
                if (this.form.first == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入模板标题',
                        offset: 70
                    })
                    this.$refs['first'].focus()
                    return false
                }

                if (this.form.keyword1 == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入关键词1',
                        offset: 70
                    })
                    this.$refs['keyword1'].focus()
                    return false
                }

                if (this.form.keyword2 == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入关键词2',
                        offset: 70
                    })
                    this.$refs['keyword2'].focus()
                    return false
                }

                if (this.form.remark == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入模板备注',
                        offset: 70
                    })
                    this.$refs['remark'].focus()
                    return false
                }

                const loading = this.$loading({
                    lock: true,
                    text: '正在批量操作中，请等待...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })

                post('/sendTemplate', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        loading.close();
                        this.$notify.success({
                            title: '系统提示',
                            message: '模板消息发送成功',
                            offset: 70
                        })

                    }
                }).catch()
            }
        }
    }
</script>

<style>
</style>